<!--
 * @Descripttion: 
 * @Author: YuZhenJing
 * @Date: 2020-01-31 20:15:54
 * @LastEditors: YuZhenJing
 * @LastEditTime: 2020-03-20 15:25:00
 -->
<template>
  <div class="org-edit">
    <tf-dialog
      :title="windowInfo.title"
      :title-icon="windowInfo.titleIcon"
      :visible.sync="tfVisible"
      :showClose="!saveLoading"
    >
      <template v-slot:content>
        <el-card shadow="never">
          <el-form
            :model="formItem"
            :rules="rules"
            label-width="120px"
            ref="orgFrom"
          >
            <el-row :gutter="12">
              <el-col :span="24">
                <el-form-item label="排序标识" prop="order">
                  <el-tooltip placement="top">
                    <div slot="content"><strong>格式:</strong> [001-999]</div>
                    <el-input v-model="formItem.order">
                      <template
                        v-if="formItem.parentOrgOrderNumber != ''"
                        slot="prepend"
                        >{{ formItem.parentOrgOrderNumber }}</template
                      >
                    </el-input>
                  </el-tooltip>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="本级机构名称" prop="orgName">
                  <el-input v-model="formItem.orgName"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="组织机构状态">
                  <el-input
                    v-model="formItem.orgStatusName"
                    disabled
                  ></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-card>
      </template>
      <template v-slot:footer>
        <el-row :gutter="12" style="padding:5px;">
          <el-col :span="24" style="text-align:center">
            <el-button
              :loading="saveLoading"
              @click="submitForm"
              icon="el-icon-circle-plus-outline"
              type="primary"
              >保存</el-button
            >
          </el-col>
        </el-row>
      </template>
    </tf-dialog>
  </div>
</template>
<script>
export default {
  components: {
    'tf-dialog': require('@/components/common/dialog/TfDialog').default
  },
  data() {
    //验证需要不能是0000
    let orderFormat = (rule, value, callback) => {
      if (value == '000') {
        return callback(new Error('排序标识格式不正确，格式为:[001-999]!'))
      } else {
        return callback()
      }
    }
    return {
      tfVisible: false,
      saveLoading: false,
      // 窗体信息
      windowInfo: {
        titleIcon: 'el-icon-edit',
        title: '编辑组织机构'
      },
      formItem: {
        parentOrgOrderNumber: '',
        parentOrgName: '',
        order: '',
        orgOrderNumber: '',
        orgName: '',
        orgStatusName: '',
        orgCode: ''
      },
      rules: {
        order: [
          { required: true, message: '排序标识不能为空!', trigger: 'blur' },
          {
            pattern: /^\d{3}$/,
            message: '排序标识格式不正确，格式为:[001-999]!',
            trigger: 'blur'
          },
          { validator: orderFormat, trigger: 'blur' }
        ],
        orgName: [
          { required: true, message: '组织机构名称不能为空!', trigger: 'blur' },
          {
            max: 50,
            message: '组织机构名称不得超过【50】个字符',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    show(data) {
      if (data.orgCode) {
        if (this.$refs.orgFrom) {
          this.$refs.orgFrom.resetFields()
        }
        let numberLength = data.orgOrderNumber.length
        if (numberLength > 3) {
          this.formItem.parentOrgOrderNumber = data.orgOrderNumber.substring(
            0,
            numberLength - 3
          )
          this.formItem.order = data.orgOrderNumber.substring(
            numberLength - 3,
            numberLength
          )
        } else {
          this.formItem.order = data.orgOrderNumber
        }
        this.formItem.orgCode = data.orgCode
        this.formItem.orgName = data.orgName
        this.formItem.orgStatusName = data.orgStatusName
        this.tfVisible = true
      } else {
        this.$alert('未找到需要编辑的相关信息，无法进行编辑', '提示', {
          type: 'warning',
          confirmButtonText: '确定'
        })
      }
    },
    submitForm() {
      this.$refs.orgFrom.validate(valid => {
        if (valid) {
          this.saveLoading = true
          if (this.formItem.parentOrgOrderNumber) {
            this.formItem.orgOrderNumber =
              this.formItem.parentOrgOrderNumber + this.formItem.order
          } else {
            this.formItem.orgOrderNumber = this.formItem.order
          }
          const saveFormItem = this.$html.post('org/saveEditOrg', this.formItem)
          saveFormItem
            .then(data => {
              if (data.returnType == 'success') {
                this.$emit('orgTreeData')
                this.saveLoading = false
                this.$notify({
                  title: '成功',
                  message: '保存成功',
                  type: 'success'
                })
                this.tfVisible = false
              }
            })
            .catch(() => {
              this.saveLoading = false
              console.log('error:orgEdit/submitForm')
            })
        } else {
          return false
        }
      })
    }
  }
}
</script>
